import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';

export default class EntryAbility extends UIAbility {
  // 为参数添加类型声明
  onCreate(want: any, launchParam: any) {
    hilog.info(0x0000, 'Ability', '%{public}s', 'Ability onCreate');
    // 初始化应用数据
    this.initializeAppData();
  }

  onDestroy() {
    hilog.info(0x0000, 'Ability', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'Ability', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/CaseListPage', (err: any, data: any) => {
      if (err?.code) {
        hilog.error(0x0000, 'Ability', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
        return;
      }
      hilog.info(0x0000, 'Ability', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data));
    });
  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'Ability', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground() {
    // Ability has brought to foreground
    hilog.info(0x0000, 'Ability', '%{public}s', 'Ability onForeground');
  }

  onBackground() {
    // Ability has back to background
    hilog.info(0x0000, 'Ability', '%{public}s', 'Ability onBackground');
    // 保存应用状态
    this.saveAppState();
  }

  private initializeAppData(): void {
    // 初始化应用数据，如案件列表、用户统计等
    hilog.info(0x0000, 'Ability', '%{public}s', 'Initializing app data');
  }

  private saveAppState(): void {
    // 保存应用状态
    hilog.info(0x0000, 'Ability', '%{public}s', 'Saving app state');
  }
}